package com.suning.sawp.service.impl.bi;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.suning.framework.dal.client.DalClient;
import com.suning.sawp.constants.LoggerConstants;
import com.suning.sawp.intf.bi.BiBrandDaoService;
import com.suning.sawp.po.bi.BiBrandSale;
import com.suning.sawp.service.util.DateUtils;

@Service("biBrandDaoService")
public class BiBrandDaoServiceImpl implements BiBrandDaoService {

    private static final Logger LOGGER = LoggerFactory.getLogger(LoggerConstants.SAWP_BI);

    private static final String QUERY_BRANDSALE_DAY = "BI_BRAND_SQL.QUERY_BRANDSALE_DAY";
    private static final String QUERY_BRANDSALE_MONTH = "BI_BRAND_SQL.QUERY_BRANDSALE_MONTH";
    
    private static final String ORDER_BY_AMOUNT = "0";

    private static final String ORDER_BY_CNT = "1";

    @Resource
    DalClient dalClient;

    @Override
    public List<BiBrandSale> queryBrandSaleByDay(String cateCode, String queryTime, String storeCode,
            String orderCondition) {
        LOGGER.info("queryBrandSaleByDay ,cateCode:{},queryTime:{},storeCode:{}", cateCode, queryTime, storeCode);
        Map<String, Object> paramMap = new HashMap<String, Object>();
        if (StringUtils.isNotBlank(cateCode)) {
            paramMap.put("cateCode", cateCode);
        }else{
            paramMap.put("needLimit", "1");
        }

        if (ORDER_BY_AMOUNT.equals(orderCondition)) {
            paramMap.put("order", ORDER_BY_AMOUNT);
        } else {
            paramMap.put("order", ORDER_BY_CNT);
        }

        // 转化时间格式，YYYY-MM-DD转化成YYYYMMDD格式
        paramMap.put("queryTime", DateUtils.getDateStrByFormat(
                DateUtils.getDateByStr(queryTime, DateUtils.DEFAULT_DAY_PATTERN), DateUtils.YYYYMMDD_PATTERN));
        paramMap.put("storeCode", storeCode);
        return dalClient.queryForList(QUERY_BRANDSALE_DAY, paramMap, BiBrandSale.class);
    }
    
    @Override
    public List<BiBrandSale> queryBrandSaleByDay(String cateCode, String brandCode, String queryTime, String storeCode,
            String orderCondition) {
        LOGGER.info("queryBrandSaleByDay ,cateCode:{},queryTime:{},storeCode:{},brandCode:{}", cateCode, queryTime, storeCode,brandCode);
        Map<String, Object> paramMap = new HashMap<String, Object>();
        if (StringUtils.isNotBlank(cateCode)) {
            paramMap.put("cateCode", cateCode);
        }else{
            paramMap.put("needLimit", "1");
        }
        if (StringUtils.isNotBlank(brandCode)) {
            paramMap.put("brandCode", brandCode);
        }
        if (ORDER_BY_AMOUNT.equals(orderCondition)) {
            paramMap.put("order", ORDER_BY_AMOUNT);
        } else {
            paramMap.put("order", ORDER_BY_CNT);
        }
        paramMap.put("queryTime", queryTime);
        paramMap.put("storeCode", storeCode);
        return dalClient.queryForList(QUERY_BRANDSALE_DAY, paramMap, BiBrandSale.class);
    }

    @Override
    public List<BiBrandSale> queryBrandSaleByMonth(String cateCode, String queryTime, String storeCode,
            String orderCondition) {

        LOGGER.info("queryBrandSaleByMonth ,cateCode:{},queryTime:{},storeCode:{}", cateCode, queryTime, storeCode);
        Map<String, Object> paramMap = new HashMap<String, Object>();
        if (StringUtils.isNotBlank(cateCode)) {
            paramMap.put("cateCode", cateCode);
        }else{
            paramMap.put("needLimit", "1");
        }

        if (ORDER_BY_AMOUNT.equals(orderCondition)) {
            paramMap.put("order", ORDER_BY_AMOUNT);
        } else {
            paramMap.put("order", ORDER_BY_CNT);
        }

        // 转化时间格式，YYYY-MM转化成YYYYMM格式
        paramMap.put("queryTime", DateUtils.getDateStrByFormat(
                DateUtils.getDateByStr(queryTime, DateUtils.DEFAULT_MONTH_PATTERN), DateUtils.YYYYMM_PATTERN));
        paramMap.put("storeCode", storeCode);
        return dalClient.queryForList(QUERY_BRANDSALE_MONTH, paramMap, BiBrandSale.class);

    }
    
    @Override
    public List<BiBrandSale> queryBrandSaleByMonth(String cateCode, String brandCode, String queryTime, String storeCode,
            String orderCondition) {

        LOGGER.info("queryBrandSaleByMonth ,cateCode:{},queryTime:{},storeCode:{}", cateCode, queryTime, storeCode);
        Map<String, Object> paramMap = new HashMap<String, Object>();
        if (StringUtils.isNotBlank(cateCode)) {
            paramMap.put("cateCode", cateCode);
        }else{
            paramMap.put("needLimit", "1");
        }
        if (StringUtils.isNotBlank(brandCode)) {
            paramMap.put("brandCode", brandCode);
        }
        if (ORDER_BY_AMOUNT.equals(orderCondition)) {
            paramMap.put("order", ORDER_BY_AMOUNT);
        } else {
            paramMap.put("order", ORDER_BY_CNT);
        }
        paramMap.put("queryTime", queryTime);
        paramMap.put("storeCode", storeCode);
        return dalClient.queryForList(QUERY_BRANDSALE_MONTH, paramMap, BiBrandSale.class);

    }
}
